/*
 * to use online script you can use jquery getScript plugin as follows:
 * 
 * $.getScript('http://sisense.com/abc.js', function(data, textStatus)
 * {
 *		// your code which requires abc.js here
 * });
 * 
 * // more info at: http://api.jquery.com/jQuery.getScript/
 *
 */


/*
 * set to true when replacing widget rendering engine.
 */
this.isCustom = true;

/*
 * exectuted after the widget controller was initialized, useful when overriding entire widget in order to instantiate entirely different implementation.
 *
 * @param {args} wraps all function arguments, including widget, dashboard and widget controller instances.
 *
 */
this.init = function (args) {

    $('head').append('<link href="http://testdude/merck.css" rel="stylesheet" type="text/css" />');
};

/*
 * executed before the widget query result is loaded to the DOM and allows canceling such beavior and replace it with custom widget implementation.
 * to cancel the default population just set 'args.cancel = true' and the widget default population will be ignored.
 *
 * @param {args} wraps all function arguments, including query result, widget, dashboard and widget controller instances.
 *
 */
this.beforeResultLoaded = function (args) {

    // -- Define Slave Key
    var options = {

        templateFile: '/box.ejs',
        cssFile: '/merck.css',
        slaveKey: 'Financial',
        slaveCount: 3,
        browseToUrl: '/dashboards/'
    };
    options.slaveTrigger = 'finishedSync_' + options.slaveKey;

    // -- Apppend css file to page
    $('head').append('<link href="' + options.cssFile + '" rel="stylesheet" type="text/css" />');

    // -- Empty existing pivot
    var containerId = $(args.element).attr('id');
    var container = $('#'+containerId);
    container.empty();
    container.closest('tbody').find('.rdTop').empty(); // remove title

    var db = args.widget.dashboard;

    /**
     *  Define Master
     */
    var master = function () {

        /**
         * A function to render the master
         */
        var renderBoard = function () {

            var box = {

                title: args.widget.title,
                items: db.sync[options.slaveKey].slaves
            };

            var renderedEl = $.View(options.templateFile, { box: box } );
            container.append(renderedEl);

            $(container.find('.goToDashboard')[0]).bind('click', function () {

                parent.location.href = options.browseToUrl;
            })
        };

        /**
         *  Renders the master when the slaves are ready
         */
        if (db.sync && db.sync[options.slaveKey] && (db.sync[options.slaveKey].slaves.length == options.slaveCount))
            renderBoard();
        else {
            db.bind(slaveTrigger, function (event, key) {

                if (db.sync[options.slaveKey].slaves.length == options.slaveCount)
                    renderBoard();
            });
        }
    };

    /**
     *  Run Master
     */
    master();
};

/*
 * executed after the widget result were added to the DOM. useful to apply DOM tweaks after query result population.
 *
 * @param {args} wraps all function arguments, including query result, widget, dashboard and widget controller instances.
 *
 */
this.afterResultLoaded = function (args) {


};

/*
 * exectuted after all dashboard widget models were initialiezed. useful for binding with other widget events.
 *
 * @param {args} wraps all function arguments, including widget, dashboard and widget controller instances.
 *
 */
this.handleDashboardInitialized = function (args) {
};
